package com.wxtl.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Service;

import com.wxtl.action.qo.LoginUser;
import com.wxtl.dao.AgentDAO;
import com.wxtl.dao.LkAgentProductDAO;
import com.wxtl.dao.ProductDAO;
import com.wxtl.entity.Agent;
import com.wxtl.entity.LkAgentProduct;
import com.wxtl.entity.Product;
import com.wxtl.exception.ServiceException;
import com.wxtl.util.Constants;
import com.wxtl.util.DataGridModel;
import com.wxtl.util.ID;
import com.wxtl.util.MD5Util;
import com.wxtl.util.Page;
import com.wxtl.util.Status;

@Service
public class AgentService {
	private AgentDAO agentDAO;
	private LkAgentProductDAO lkAgentProductDAO;
	private ProductDAO productDAO;
	
	public AgentDAO getAgentDAO() {
		return agentDAO;
	}

	public void setAgentDAO(AgentDAO agentDAO) {
		this.agentDAO = agentDAO;
	}

	public void setProductDAO(ProductDAO productDAO) {
		this.productDAO = productDAO;
	}

	public void setLkAgentProductDAO(LkAgentProductDAO lkAgentProductDAO) {
		this.lkAgentProductDAO = lkAgentProductDAO;
	}

	public Page query(DataGridModel dgm,Agent agent){
		return agentDAO.query(dgm,agent);
	}
	
	public void delete(int[] ids){
		agentDAO.delete(ids);
	}
	
	public Agent getAgentById(int id){
		return agentDAO.load(id);
	}
	
	public Agent add(Agent agent){
		agent.setAgentsn(ID.agentId());//设置代理商的ID
		agent.setPassword(MD5Util.getMD5String(agent.getPassword()+Constants.SALT));//密码加密
		agent.setState(Status.AGENT_STATE_NORMAL);
		agent.setRegTime(new Date());
		agentDAO.add(agent);
		return agent;
	}
	
	public void update(Agent agent){
		agentDAO.update(agent);
	}
	
	public Agent getAgentByNameAndPwd(LoginUser loginUser){
		//设置md5的密码
		//loginUser.setPassword(MD5Util.getMD5String(loginUser.getPassword()+Constants.SALT));
		//System.out.println("AgentService.java:user:"+loginUser.getUsername()+"\tpass:"+loginUser.getPassword());
		//根据加密因子设置密码
		loginUser.setPassword(MD5Util.getMD5String(loginUser.getPassword()+Constants.SALT));
		Agent agent = agentDAO.getAgentByNameAndPwd(loginUser);
		if(agent==null || agent.getState()==Status.AGENT_STATE_DELETE)
			throw new ServiceException("用户名或密码不存在");
		if(agent.getState()==Status.AGENT_STATE_BLOCK)
			throw new ServiceException("用户已锁定，请联系管理员");
		
		return agent;
	}
	
	public Page queryProduct(DataGridModel dgm,int agentId){
		Page page=lkAgentProductDAO.query(dgm, agentId);;
		List<Integer> idList=new ArrayList<Integer>();
		List<LkAgentProduct> list=page.getRows();
		for(int i=0;i<list.size();i++){
			idList.add(list.get(i).getId().getProductId());
		}
		List<Product> proList=productDAO.query(idList.toArray(new Integer[]{}));
		page.setRows(proList);
		return page;
	}
	
	public Agent queryAgentById(int id){
		return agentDAO.getAgentById(id);
	}
	
	public List<Agent> getAllAgent(){
		return agentDAO.queryAllAgent();
	}
}
